package com.artisan;

import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.ImageContent;
import dev.langchain4j.data.message.TextContent;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.output.Response;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;

import static dev.langchain4j.model.openai.OpenAiChatModelName.GPT_4_O_MINI;

public class Test {
    public static void main(String[] args) {

        test01();
    }



    public static void test03(){
        try {
            byte[] imageBytes = Files.readAllBytes(Paths.get("D:\\1.png"));
            String base64Data = Base64.getEncoder().encodeToString(imageBytes);
            ImageContent imageContent = ImageContent.from(base64Data, "image/png");
            UserMessage userMessage = UserMessage.from(imageContent);
            // 处理逻辑
            System.out.println("User Message processed: " + userMessage);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void test02(){
        // 定义API密钥，此处使用示例密钥"demos"
        String apiKey = "demo";

        // 构建OpenAI聊天模型实例，指定模型名称为GPT_4_O_MINI，并提供API密钥
        ChatLanguageModel model = OpenAiChatModel.builder()
                .modelName(GPT_4_O_MINI)
                .apiKey(apiKey)
                .build();


        UserMessage userMessage = UserMessage.from(
                TextContent.from("描述下面这幅画"),
                ImageContent.from("http://gips3.baidu.com/it/u=3886271102,3123389489&fm=3028&app=3028&f=JPEG&fmt=auto?w=1280&h=960")
        );
        Response<AiMessage> response = model.generate(userMessage);
        System.out.println(response.content().text());
    }

    public static void test01(){
        // 定义API密钥，此处使用示例密钥"demos"
        String apiKey = "demo";

        // 构建OpenAI聊天模型实例，指定模型名称为GPT_4_O_MINI，并提供API密钥
        ChatLanguageModel model = OpenAiChatModel.builder()
                .modelName(GPT_4_O_MINI)
                .apiKey(apiKey)
                .build();

        UserMessage firstUserMessage = UserMessage.from("你好, 我的名字是小工匠");
        AiMessage firstAiMessage = model.generate(firstUserMessage).content();
        // AI 回复: 你好，小工匠！很高兴认识你。有什麼我可以帮你的吗？
        System.out.println(firstAiMessage.text());

        UserMessage secondUserMessage = UserMessage.from("我叫什么名字?");
        AiMessage secondAiMessage = model.generate(firstUserMessage, firstAiMessage, secondUserMessage).content();
        // AI 回复: 你叫小工匠！如果你有其他问题或想聊些什么，随时告诉我！
        System.out.println(secondAiMessage.text());
    }
}
